<?php

/*
 * Controller/VariableController.php
 */

namespace RHX\Controller;

use Zend\View\Model\JsonModel;

class BddSalariesController extends RHXController {

    public function indexAction() {
	 // Paramètres        
        $refresh = $this->params()
                ->fromRoute('other1', 0);
        $page = $this->params()
                ->fromRoute('other2', 1);
        $perPage = $this->params()
                ->fromRoute('other3',  ($defaultPerPage  = 30 ) );
        if ($perPage <= 0)
            $perPage = $defaultPerPage;
        
        // Tables
        $siteTable = $this->getService('Table\SiteTable');

        // SiteList
        $siteList = $siteTable->getAll();
        
        // liste type de contrat
        $tableContratType = new \RHX\Model\Table\ContratTypeTable($this->getAdapter());
        $listContratType  = $tableContratType->getAll('id ASC');
        
        // liste poste
        $tablePoste = new \RHX\Model\Table\PosteTable($this->getAdapter());
        $listPoste = $tablePoste->getAll();
         
        return array(            
            'refresh' => ($refresh == 0) ? false : true,
            'siteList' => $siteList,
            'page' => $page,
            'perPage' => $perPage,
            'listContratType' => $listContratType,
            'listPoste' => $listPoste 
        );
    }
    
    /* Charger la liste des employé
     * 
     * 
     * */
    
     public function loadIndexAction() {
        // Changement de Layout
        $this->layout('layout/ajax');

        // Paramètres        
        $page = (int )$this->params()
                ->fromRoute('other1', 1);
        
        $perPage = (int) $this->params()
                ->fromRoute('other2', $defaultPerPage = 30);
        
        if ($perPage <= 0)
            $perPage = $defaultPerPage;
        
        // Param filtre
        $nom = $this->getRequest()->getPost('nom', null);
        $contratType = $this->getRequest()->getPost('contrat_type', null);
        $poste = $this->getRequest()->getPost('poste', -1);
        $orderCol = $this->getRequest()->getPost('order_col', 'matricule');
        $orderColState = $this->getRequest()->getPost('order_col_state', 'asc');
        $actif =  $this->getRequest()->getPost('actif', true);
        $siteId =  $this->getRequest()->getPost('site', -1);
        
        // Tables
        $employeTable = $this->getService('Table\EmployeTable');
        $siteTable = $this->getService('Table\SiteTable');
        
        if($orderCol && $orderColState){
            $order = $orderCol . " " . $orderColState; 
        }
        else{
            $order = 'matricule ASC';
        }

        // Liste des employés du Site
        $employeList = $employeTable->getEmployesByCriteria($siteId, $order, $page, $perPage, $actif, $nom, $contratType, $poste);   
        
        // Nombre total d'employe
        $countEmployeTotal = $employeTable->getCountEmployesByCriteria($siteId, $actif, $nom, $contratType, $poste);
            //\Zend\Debug\Debug::dump('total empl '. $countEmployeTotal); \Zend\Debug\Debug::dump('total count empl '. count($employeList));
        // Nombre de page
        $nbPage = (int) ceil($countEmployeTotal/$perPage);
       
        return array(
            'employeList' => $employeList,            
            'nbPage' => $nbPage,
            'page' => $page,            
            'orderCol' => $orderCol,
            'orderColState' => $orderColState,
        );
    }
    
    /* Cahrger la fiche employé
     * 
     * @return array
     * */
    
     public function employeAction() {
         //parameter
         
         $matricule = (int) $this->params()
                        ->fromRoute('id', 0);
         
         return array(
            'matricule' => $matricule,                    
        );
    }
    
    
    
}


?>
